package com.fashion.web.actions;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.fashion.model.News;
import com.fashion.model.NewsColumn;
import com.fashion.service.ColumnService;
import com.fashion.service.NewsService;
import com.fashion.utils.NewsTemplate;
import com.opensymphony.xwork2.ActionSupport;

@Scope("prototype")
@Controller
@Namespace("/fashion")
public class FashionAction extends ActionSupport {
	List<News> focusNews = new ArrayList<News>();
	List<News> clothNews = new ArrayList<News>();
	List<News> specialNews = new ArrayList<News>();
	List<News> videoNews = new ArrayList<News>();
	List<News> hairdressingNews = new ArrayList<News>();
	List<News> bagNews = new ArrayList<News>();
	List<News> shoeNews = new ArrayList<News>();
	List<News> perfumeNews = new ArrayList<News>();
	List<News> adornmentNews = new ArrayList<News>();
	List<News> fashionSpecialNews = new ArrayList<News>();
	List<News> activityNews = new ArrayList<News>();
    private  NewsColumn column;
    private static final Logger logger = Logger.getLogger(FashionAction.class);

    @Resource(name = "newsService")
	private NewsService newsService;
	@Resource(name = "columnService")
	private ColumnService columnService;

	@Action(value = "index", results = { @Result(name = SUCCESS, location = "index.jsp"), @Result(name = ERROR, location = "../error.jsp") })
	public String index() throws Exception {
        try {
            List<NewsTemplate> templates = new ArrayList<NewsTemplate>();
            // foucs news
            templates.add(new NewsTemplate(5, 6));
            NewsColumn focusColumn = columnService.findColumnByUrl("fashion",
                    "fashion");
            column = focusColumn;
            List<NewsColumn> focusColumns = columnService
                    .getAllChildren(focusColumn);
            focusColumns.add(focusColumn);
            focusNews = newsService.searchNewsByColumnListAndTemplateList(
                    focusColumns, templates, 1);

            // clothNews
            templates.clear();
            templates.add(new NewsTemplate(4, 2));
            templates.add(new NewsTemplate(3, 3));
            templates.add(new NewsTemplate(2, 6));
            NewsColumn clothColumn = columnService.findColumnByUrl("fashion",
                    "fushi");
            List<NewsColumn> clothColumns = columnService
                    .getAllChildren(clothColumn);
            clothColumns.add(clothColumn);
            clothNews = newsService.searchNewsByColumnListAndTemplateList(
                    clothColumns, templates, 1);
            // specialNews
            templates.clear();
            templates.add(new NewsTemplate(4, 1));
            templates.add(new NewsTemplate(3, 2));
            templates.add(new NewsTemplate(2, 3));
            NewsColumn specialColumn = columnService.findColumnByUrl("special",
                    "fashion");
            List<NewsColumn> specialColumns = columnService
                    .getAllChildren(specialColumn);
            specialColumns.add(specialColumn);
            specialNews = newsService.searchNewsByColumnListAndTemplateList(
                    specialColumns, templates, 1);

            templates.clear();
            NewsColumn fashionSpeciaColumn = columnService.findColumnByUrl(
                    "special", "fashion");
            List<NewsColumn> fashionSpecialColumns = columnService
                    .getAllChildren(fashionSpeciaColumn);
            fashionSpecialColumns.add(fashionSpeciaColumn);
            templates.add(new NewsTemplate(4, 4));
            fashionSpecialNews = newsService.searchNewsByColumnListAndTemplateList(
                    fashionSpecialColumns, templates, 1);

            // videoNews
            templates.clear();
            templates.add(new NewsTemplate(4, 1));
            NewsColumn videoColumn = columnService.findColumnByUrl("video",
                    "fashion");
            List<NewsColumn> videoColumns = columnService
                    .getAllChildren(videoColumn);
            videoColumns.add(videoColumn);
            videoNews = newsService.searchNewsByColumnListAndTemplateList(
                    videoColumns, templates, 1);
            // hairdressingNews
            templates.clear();
            templates.add(new NewsTemplate(4, 2));
            templates.add(new NewsTemplate(3, 6));
            NewsColumn shoeColumn = columnService.findColumnByUrl("fashion",
                    "meixie");
            List<NewsColumn> shoeColumns = columnService.getAllChildren(shoeColumn);
            shoeColumns.add(shoeColumn);
            shoeNews = newsService.searchNewsByColumnListAndTemplateList(
                    shoeColumns, templates, 1);
            // bagNews
            templates.clear();
            templates.add(new NewsTemplate(4, 1));
            templates.add(new NewsTemplate(3, 2));
            templates.add(new NewsTemplate(2, 5));
            NewsColumn bagColumn = columnService.findColumnByUrl("fashion",
                    "xiangbao");
            List<NewsColumn> bagColumns = columnService.getAllChildren(bagColumn);
            bagColumns.add(bagColumn);
            bagNews = newsService.searchNewsByColumnListAndTemplateList(bagColumns,
                    templates, 1);
            // shoeNews
            templates.clear();
            templates.add(new NewsTemplate(4, 2));
            templates.add(new NewsTemplate(3, 4));
            templates.add(new NewsTemplate(2, 2));
            templates.add(new NewsTemplate(1, 12));
            NewsColumn hairdressingColumn = columnService.findColumnByUrl(
                    "fashion", "meirong");
            List<NewsColumn> hairdressingColumns = columnService
                    .getAllChildren(hairdressingColumn);
            hairdressingColumns.add(hairdressingColumn);
            hairdressingNews = newsService.searchNewsByColumnListAndTemplateList(
                    hairdressingColumns, templates, 1);
            // perfumeNews
            templates.clear();
            templates.add(new NewsTemplate(4, 1));
            templates.add(new NewsTemplate(3, 3));
            NewsColumn perfumeColumn = columnService.findColumnByUrl("fashion",
                    "xiangshui");
            List<NewsColumn> perfumeColumns = columnService
                    .getAllChildren(perfumeColumn);
            perfumeColumns.add(perfumeColumn);
            perfumeNews = newsService.searchNewsByColumnListAndTemplateList(
                    perfumeColumns, templates, 1);
            // adornmentNews
            templates.clear();
            templates.add(new NewsTemplate(4, 1));
            templates.add(new NewsTemplate(3, 3));
            NewsColumn adornmentColumn = columnService.findColumnByUrl("fashion",
                    "peishi");
            List<NewsColumn> adornmentColumns = columnService
                    .getAllChildren(adornmentColumn);
            adornmentColumns.add(adornmentColumn);
            adornmentNews = newsService.searchNewsByColumnListAndTemplateList(
                    adornmentColumns, templates, 1);
            // 活动
            templates.clear();
            NewsColumn activityColumn = columnService.findColumnByUrl("activity",
                    "activity");
            List<NewsColumn> activityColumns = columnService
                    .getAllChildren(activityColumn);
            activityColumns.add(activityColumn);
            templates.add(new NewsTemplate(4, 2));
            templates.add(new NewsTemplate(3, 4));
            activityNews = newsService.searchNewsByColumnListAndTemplateList(
                    activityColumns, templates, 2);
        } catch (Exception e) {
            logger.error("exception in FashionIndexAction: ",e);

            return ERROR;
        }

        return SUCCESS;
	}

	public List<News> getFocusNews() {
		return focusNews;
	}

	public List<News> getClothNews() {
		return clothNews;
	}

	public List<News> getSpecialNews() {
		return specialNews;
	}

	public List<News> getVideoNews() {
		return videoNews;
	}

	public List<News> getHairdressingNews() {
		return hairdressingNews;
	}

	public List<News> getBagNews() {
		return bagNews;
	}

	public List<News> getShoeNews() {
		return shoeNews;
	}

	public List<News> getPerfumeNews() {
		return perfumeNews;
	}

	public List<News> getAdornmentNews() {
		return adornmentNews;
	}

	public List<News> getFashionSpecialNews() {
		return fashionSpecialNews;
	}

	public List<News> getActivityNews() {
		return activityNews;
	}

    public NewsColumn getColumn() {
        return column;
    }

    public void setColumn(NewsColumn column) {
        this.column = column;
    }
}
